package com.shanyue.redwine.activity;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.databinding.ViewDataBinding;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;

import com.shanyue.redwine.R;
import com.shanyue.redwine.http.RestClient;
import com.shanyue.redwine.utils.AppTaskManager;
import com.shanyue.redwine.utils.Utils;
import com.tbruyelle.rxpermissions2.RxPermissions;

import cn.pedant.SweetAlert.SweetAlertDialog;
import rx.subscriptions.CompositeSubscription;

/**
 * 基本封装activity基类
 */
public abstract class ActivityBase<T extends ViewDataBinding> extends AppCompatActivity {
    SweetAlertDialog mLoadingDialog;
    RestClient mRestClient;
    CompositeSubscription mCompositeSubscription;
    Fragment mFragment;
    T mBinding;
    Activity mActivity;
    RxPermissions mRxPermissions;
    public static boolean isForeground = false;

    public String getTAG() {
        return this.getClass().getSimpleName();
    }

    public String TAG;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams
                .SOFT_INPUT_STATE_HIDDEN);
        mActivity = this;
        AppTaskManager.getAppManager().addActivity(this);
        TAG = getTAG();
        mRestClient = new RestClient(this);
        mLoadingDialog = new SweetAlertDialog(this, SweetAlertDialog.PROGRESS_TYPE).setTitleText(getString(R.string
                .LOADING));
        mLoadingDialog.setCancelable(true);
        mLoadingDialog.setCanceledOnTouchOutside(true);
        //点击取消的时候取消订阅
        mLoadingDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                mRestClient.cancelTag(TAG);
            }
        });
    }

    Context getCurrentContext() {
        return this;
    }

    public void showLoadingDialog() {
        mLoadingDialog.show();
    }

    public void dismissLoadingDialog() {
        mLoadingDialog.dismiss();
    }

    public void removeSubscription() {
        if (this.mCompositeSubscription != null && mCompositeSubscription.hasSubscriptions()) {
            this.mCompositeSubscription.unsubscribe();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (this.mCompositeSubscription != null && mCompositeSubscription.hasSubscriptions()) {
            this.mCompositeSubscription.unsubscribe();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    /**
     * 弹出toast
     *
     * @param msg
     */
    void showToast(String msg) {
        Utils.showToast(this, msg);
    }
}
